﻿Imports System.Data.SqlClient


Public Class FarmaciaDAO

    Public Shared Function insertarFarmacia(ByVal FarmaciaNombre As String, ByVal FarmaciaRazonSocial As String, ByVal FarmaciaEmail As String, ByVal FarmaciaTelefono As String, ByVal FarmaciaCodigoPAMI As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            Constante.esNulo(FarmaciaNombre)
            Constante.esNulo(FarmaciaRazonSocial)
            Constante.esNulo(FarmaciaTelefono)
            Constante.esNulo(FarmaciaEmail)
            Constante.esNulo(FarmaciaCodigoPAMI)
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_farmacia"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaNombre", FarmaciaNombre).DbType = DbType.String
                    .AddWithValue("@FarmaciaRazonSocial", FarmaciaRazonSocial).DbType = DbType.String
                    .AddWithValue("@FarmaciaTelefono", FarmaciaTelefono).DbType = DbType.String
                    .AddWithValue("@FarmaciaEmail", FarmaciaEmail).DbType = DbType.String
                    .AddWithValue("@FarmaciaCodigoPAMI", FarmaciaCodigoPAMI).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerFarmacias(ByVal FarmaciaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_farmacia_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
            da.Fill(ds, "Farmacias")

            Return ds.Tables("Farmacias")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
    Public Shared Function obtenerFarmaciasDs(ByVal FarmaciaId As Integer) As DataSet
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_farmacia_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
            da.Fill(ds, "Farmacias")

            Return ds
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerFarmaciaId(ByVal FarmaciaCodigoPAMI As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "Select FarmaciaId From Farmacia Where FarmaciaCodigoPAMI = " & FarmaciaCodigoPAMI.ToString
            Dim FarmaciaId As Integer = cmd.ExecuteScalar

            Return FarmaciaId
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarFarmacia(ByVal FarmaciaId As Integer, ByVal FarmaciaNombre As String, ByVal FarmaciaRazonSocial As String, ByVal FarmaciaTelefono As String, ByVal FarmaciaEmail As String, ByVal FarmaciaCodigoPAMI As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            Constante.esNulo(FarmaciaNombre)
            Constante.esNulo(FarmaciaRazonSocial)
            Constante.esNulo(FarmaciaTelefono)
            Constante.esNulo(FarmaciaEmail)
            Constante.esNulo(FarmaciaCodigoPAMI)
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_farmacia"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaNombre", FarmaciaNombre).DbType = DbType.String
                    .AddWithValue("@FarmaciaRazonSocial", FarmaciaRazonSocial).DbType = DbType.String
                    .AddWithValue("@FarmaciaTelefono", FarmaciaTelefono).DbType = DbType.String
                    .AddWithValue("@FarmaciaEmail", FarmaciaEmail).DbType = DbType.String
                    .AddWithValue("@FarmaciaCodigoPAMI", FarmaciaCodigoPAMI).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarFarmacia(ByVal FarmaciaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_farmacia"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaNombre", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@FarmaciaRazonSocial", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@FarmaciaTelefono", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@FarmaciaEmail", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@FarmaciaCodigoPAMI", Constante.MENOSUNO).DbType = DbType.String
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function selectFarmacias(ByVal FarmaciaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_farmacia_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
            da.Fill(ds, "Farmacias")

            Return ds.Tables("Farmacias")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function comboFarmacia() As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_farmacia_combo", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.Fill(ds, "Farmacias")

            Return ds.Tables("Farmacias")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
End Class
